﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace MoneyWentWhere.DataModel
{
    [TypeConverter(typeof(CurrencyTypeConverter))]
    [DataContract]
    public class Currency : IComparable
    {
        [DataMember(Name = "Id")]
        public String _id;

        // For use with data binding only
        public Currency() { }

        public Currency(String id)
        {
            _id = id.ToUpper();
        }

        public String Id
        {
            get { return _id; }
        }

        public override string ToString()
        {
            return _id;
        }

        public override int GetHashCode()
        {
            return _id.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj is String && _id.Equals(obj)) { return true; }

            Currency that = obj as Currency;
            if (that == null) { return false; }

            return this.Id.Equals(that.Id);
        }

        public static bool operator ==(Currency a, Currency b)
        {
            // If both are null, or both are same instance, return true.
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            // If one is null, but not both, return false.
            if (((object)a == null) || ((object)b == null))
            {
                return false;
            }

            // Return true if the fields match:
            return a.Id == b.Id;
        }

        public static bool operator !=(Currency a, Currency b)
        {
            return !(a == b);
        }

        public static decimal ExchangeRate(decimal fromAmount, decimal toAmount)
        {
            return Math.Abs(toAmount / fromAmount);
        }

        int IComparable.CompareTo(object obj)
        {
            Currency that = obj as Currency;
            if (that == null) { return -1; }

            return this.Id.CompareTo(that.Id);
        }
    }
}
